package net.lgpage.http;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;

public abstract class SwipeRefeshAsyncTask<Result> extends ExceptionAsyncTask<Result> {

	public SwipeRefeshAsyncTask(Context context, HttpExceptionDealer dealer) {
		super(context, dealer);
	}

	public SwipeRefeshAsyncTask(Context context) {
		super(context);
	}

	SwipeRefreshLayout srl;

	public void setSwipeLayout(SwipeRefreshLayout srl){
		this.srl  = srl;
	}
	@Override
	public void onCancelled() {
		super.onCancelled();
		cancelSwipe();
	}

	private void cancelSwipe() {
		if (srl == null)
			return;
		srl.setRefreshing(false);
	}

	private void openSwipe() {
		if (srl == null)
			return;
		srl.post(new Runnable() {
			public void run() {
				srl.setRefreshing(true);
			}
		});
	}



	@Override
	public void onPostExecute(Result result) {
		super.onPostExecute(result);
		cancelSwipe();
	}

	@Override
	public void onException(Exception e) {
		super.onException(e);
		cancelSwipe();
	}

	@Override
	public void onPreExecute() {
		openSwipe();
		super.onPreExecute();
	}

}
